<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="600px">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      label-width="auto"
    >
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input v-model="formData.supplierName" placeholder="请输入供应商名称" clearable />
      </el-form-item>
      <el-form-item label="供应商负责人" prop="supplierDirector">
        <el-input v-model="formData.supplierDirector" placeholder="请输入供应商负责人" clearable />
      </el-form-item>
      <el-form-item label="供应商地址" prop="supplierAddress">
        <el-input
          v-model="formData.supplierAddress"
          type="textarea"
          placeholder="请输入供应商地址"
          clearable
        />
      </el-form-item>
      <el-form-item label="供应商电话" prop="supplierMobile">
        <el-input v-model="formData.supplierMobile" placeholder="请输入供应商电话" clearable />
      </el-form-item>
      <el-form-item label="供应商状态" prop="supplierStatus">
        <el-select v-model="formData.supplierStatus" placeholder="请选择供应商状态" clearable>
          <el-option label="使用" :value="0" />
          <el-option label="停用" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商性质" prop="supplierProperty">
        <el-select v-model="formData.supplierProperty" placeholder="请选择供应商性质" clearable>
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MATERIAL_SUPPLIER_PROPERTY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="MaterialSupplierEdit" lang="ts">
import * as MaterialSupplierApi from '@/api/material/supplier'
import * as MaterialSupplierModel from '@/model/material/info/MaterialSupplier'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
const { t } = useI18n()
const message = useMessage()
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
let formType = ''
const formData = ref({
  supplierName: undefined,
  supplierDirector: undefined,
  supplierAddress: undefined,
  supplierMobile: undefined,
  supplierStatus: 0,
  supplierProperty: undefined,
  remark: undefined
})
const formRules = reactive({
  supplierName: [{ required: true, message: '请输入供应商名称', trigger: 'blur' }],
  supplierDirector: [{ required: true, message: '请输入供应商负责人', trigger: 'blur' }],
  supplierProperty: [{ required: true, message: '请选择供应商性质', trigger: 'blur' }],
  supplierStatus: [{ required: true, message: '请选择供应商状态', trigger: 'blur' }]
})
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MaterialSupplierApi.singleGet(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MaterialSupplierModel.MaterialSupplier
    if (formType === 'create') {
      await MaterialSupplierApi.create(data)
      message.success(t('common.createSuccess'))
    } else {
      await MaterialSupplierApi.update(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    supplierName: undefined,
    supplierDirector: undefined,
    supplierAddress: undefined,
    supplierMobile: undefined,
    supplierStatus: 0,
    supplierProperty: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
